home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Inne / Gry / Carnage_Contest / scripts / CC Original / weapons / Bow.lua < prev    next >
Text File  |  2010-08-31  |  6KB  |  174 lines

  1. --------------------------------------------------------------------------------
  2. -- Weapon Bow + Projectile Arrow
  3. -- Original Carnage Contest Weapon
  4. -- Script by DC, August 2009, www.UnrealSoftware.de
  5. --------------------------------------------------------------------------------
  6.  
  7. -- Setup Tables
  8. if cc==nil then cc={} end
  9. cc.bow={}
  10. cc.bow.arrow={}
  11.  
  12. -- Load & Prepare Ressources
  13. cc.bow.gfx_wpn0=loadgfx("weapons/bow0.bmp")                    -- Weapon Image Frame 0
  14. setmidhandle(cc.bow.gfx_wpn0)
  15. cc.bow.gfx_wpn1=loadgfx("weapons/bow1.bmp")                    -- Weapon Image Frame 1
  16. setmidhandle(cc.bow.gfx_wpn1)
  17. cc.bow.gfx_pro=loadgfx("weapons/arrow.bmp")                    -- Projectile Image
  18. setmidhandle(cc.bow.gfx_pro)
  19. cc.bow.sfx_attack=loadsfx("arrow_shoot.ogg")                -- Attack Sound
  20. cc.bow.sfx_impact=loadsfx("arrow_impact.ogg")                -- Impact Sound
  21.  
  22. --------------------------------------------------------------------------------
  23. -- Weapon: bow
  24. --------------------------------------------------------------------------------
  25.  
  26. cc.bow.id=addweapon("cc.bow","Bow",cc.bow.gfx_wpn0,1)        -- Add Weapon (1 use)
  27. cc.bow.ammo=5                                                -- 5 arrows
  28.  
  29. function cc.bow.draw()                                        -- Draw
  30.     setblend(blend_alpha)
  31.     setalpha(1)
  32.     setcolor(255,255,255)
  33.     if cc.bow.ammo-weapon_shots>0 and weapon_timer<=0 then
  34.         drawinhand(cc.bow.gfx_wpn1,6,0)
  35.     else
  36.         drawinhand(cc.bow.gfx_wpn0,6,0)
  37.     end
  38.     -- Arrow
  39.     if cc.bow.ammo-weapon_shots>0 and weapon_timer<=0 and getplayeraction(0)==0 then
  40.         setrotation(getplayerrotation(0))
  41.         setscale(getplayerdirection(0),1)
  42.         drawimage(cc.bow.gfx_pro,getplayerx(0)+7*getplayerdirection(0),getplayery(0)+3)
  43.     end
  44.     -- HUD ammobar
  45.     if cc.bow.ammo-weapon_shots>0 then
  46.         hudammobar(cc.bow.ammo-weapon_shots,cc.bow.ammo)
  47.     end
  48.     -- HUD Crosshair
  49.     if cc.bow.ammo-weapon_shots>0 then
  50.         hudcrosshair(7,3)
  51.     end
  52. end
  53.  
  54. function cc.bow.attack(attack)                                -- Attack
  55.     -- Decrement timer
  56.     if weapon_timer>0 then
  57.         weapon_timer=weapon_timer-1
  58.     end
  59.     -- Attack
  60.     if weapon_shots<cc.bow.ammo and weapon_timer<=0 and attack==1 then
  61.         -- No more weapon switching!
  62.         useweapon(0)
  63.         -- Reset Timer
  64.         weapon_timer=30
  65.         -- Attack
  66.         playsound(cc.bow.sfx_attack)
  67.         weapon_shots=weapon_shots+1
  68.         id=createprojectile(cc.bow.arrow.id)
  69.         projectiles[id]={}
  70.         -- Ignore collision with current player at beginning
  71.         projectiles[id].ignore=playercurrent()
  72.         -- Set initial position of projectile
  73.         projectiles[id].x=getplayerx(0)+(7*getplayerdirection(0))-math.sin(math.rad(getplayerrotation(0)))*5.0
  74.         projectiles[id].y=getplayery(0)+3+math.cos(math.rad(getplayerrotation(0)))*5.0
  75.         -- Set speed of projectile
  76.         projectiles[id].sx=math.sin(math.rad(getplayerrotation(0)))*9.0
  77.         projectiles[id].sy=-math.cos(math.rad(getplayerrotation(0)))*9.0
  78.         -- Initial movement
  79.         projectiles[id].x=projectiles[id].x-projectiles[id].sx*1.0
  80.         projectiles[id].y=projectiles[id].y-projectiles[id].sy*1.0
  81.         for i=1,3,1 do
  82.             if cc.bow.arrow.move(id)==1 then
  83.                 break
  84.             end
  85.         end
  86.         -- Effects
  87.  
  88.         -- End Turn
  89.         if (weapon_shots>=cc.bow.ammo) then
  90.             endturn()
  91.         end
  92.     end
  93. end
  94.  
  95. --------------------------------------------------------------------------------
  96. -- Projectile: arrow
  97. --------------------------------------------------------------------------------
  98.  
  99. cc.bow.arrow.id=addprojectile("cc.bow.arrow")            -- Add Projectile
  100.  
  101. function cc.bow.arrow.draw(id)                            -- Draw
  102.     -- Setup draw mode
  103.     setblend(blend_alpha)
  104.     setalpha(1)
  105.     setcolor(255,255,255)
  106.     setscale(1,1)
  107.     -- Calculate projectile rotation
  108.     setrotation(math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy)))
  109.     -- Draw projectile
  110.     drawimage(cc.bow.gfx_pro,projectiles[id].x,projectiles[id].y)
  111.     -- Draw Arrow if out of Screen
  112.     outofscreenarrow(projectiles[id].x,projectiles[id].y)
  113. end
  114.  
  115. function cc.bow.arrow.update(id)                        -- Update
  116.     -- Wind + Gravity influence on speed
  117.     projectiles[id].sx=projectiles[id].sx+getwind()*0.1
  118.     projectiles[id].sy=projectiles[id].sy+getgravity()*1.5
  119.     -- Move
  120.     cc.bow.arrow.move(id)
  121. end
  122.  
  123. function cc.bow.arrow.move(id)
  124.     rot=math.deg(math.atan2(projectiles[id].sx,-projectiles[id].sy))
  125.     -- Move (in substep loop for optimal collision precision)
  126.     msubt=math.ceil(math.max(math.abs(projectiles[id].sx),math.abs(projectiles[id].sy))/3)
  127.     msubx=projectiles[id].sx/msubt
  128.     msuby=projectiles[id].sy/msubt
  129.     for i=1,msubt,1 do
  130.         projectiles[id].x=projectiles[id].x+msubx
  131.         projectiles[id].y=projectiles[id].y+msuby        
  132.         -- Collision
  133.         if collision(col3x3,projectiles[id].x+math.sin(math.rad(rot))*8,projectiles[id].y-math.cos(math.rad(rot))*8)==1 then
  134.             if terraincollision()==1 or objectcollision()>0 or playercollision()~=projectiles[id].ignore then
  135.                 if playercollision()~=0 then
  136.                     -- Cause Player damage
  137.                     playerpush(playercollision(),projectiles[id].sx/10.0,projectiles[id].sy/10.0)
  138.                     playerdamage(playercollision(),7)
  139.                     blood(projectiles[id].x+math.sin(math.rad(rot))*8,projectiles[id].y-math.cos(math.rad(rot))*8)
  140.                     playsound(sfx_splatter3)
  141.                 elseif objectcollision()>0 then
  142.                     -- Cause Object damage
  143.                     objectdamage(objectcollision(),7)
  144.                 else
  145.                     -- Draw Arrow in terrain
  146.                     terrainimage(cc.bow.gfx_pro,projectiles[id].x,projectiles[id].y,0,rot)
  147.                 end
  148.                 -- Effects
  149.                 playsound(cc.bow.sfx_impact)
  150.                 particle(p_smoke,projectiles[id].x+math.sin(math.rad(rot))*8,projectiles[id].y-math.cos(math.rad(rot))*8)
  151.                 particlefadealpha(0.006)
  152.                 -- Free projectile
  153.                 freeprojectile(id)
  154.                 return 1
  155.             end
  156.         else
  157.             projectiles[id].ignore=0
  158.         end
  159.         -- Water
  160.         if (projectiles[id].y)>getwatery()+5 then
  161.             -- Effects
  162.             particle(p_waterhit,projectiles[id].x,projectiles[id].y)
  163.             if math.random(1,2)==1 then
  164.                 playsound(sfx_hitwater2)
  165.             else
  166.                 playsound(sfx_hitwater3)
  167.             end
  168.             -- Free projectile
  169.             freeprojectile(id)
  170.             return 1
  171.         end
  172.     end
  173. end
  174.